package com.to.aboomy.bannersample.slice;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.HolderCreator;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.banner.ScalePageTransformer;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.util.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * @since 2021-07-26
 */
public class MultBannerAbilitySlice extends AbilitySlice implements HolderCreator {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_multbanner);
        initBanner1();
        initBanner2();
        initBanner3();
    }

    private void initBanner1() {
        Banner banner = (Banner) findComponentById(ResourceTable.Id_banner1);
        banner.setHolderCreator(this).setOuterPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {
                System.out.println("==========="+i);
            }

            @Override
            public void onPageChosen(int i) {

            }
        }).setIndicator(new IndicatorView(this).setIndicatorColor(Color.GRAY).setIndicatorSelectorColor(Color.WHITE))
        .setPageMargin(-(AttrHelper.vp2px(110,getContext())),AttrHelper.vp2px(60,getContext())).setPages(Utils.getImage(3));
    }
    private void initBanner2() {
        Banner banner2 = (Banner) findComponentById(ResourceTable.Id_banner2);
        banner2.setHolderCreator(this)
                .setIndicator(((IndicatorView) findComponentById(ResourceTable.Id_indicator2))
                        .setIndicatorColor(Color.GRAY)
                        .setIndicatorStyle(IndicatorView.INDICATOR_CIRCLE)
                        .setIndicatorSelectorColor(Color.RED), false)
                .setPageMargin(-(AttrHelper.vp2px(88,getContext())),AttrHelper.vp2px(37,getContext()))
                .setPageTransformer(true, new ScalePageTransformer())
                .setPages(Utils.getImage(5));
        banner2.setAutoPlay(true);
    }
    private void initBanner3() {
        Banner banner3 = (Banner) findComponentById(ResourceTable.Id_banner3);
        banner3.setIndicator(new IndicatorView(this)
                .setIndicatorColor(Color.GRAY)
                .setIndicatorSelectorColor(Color.WHITE)
                .setIndicatorStyle(IndicatorView.INDICATOR_CIRCLE_RECT));
        banner3.setHolderCreator(this);
        banner3.setPageMargin(-370,100);
        banner3.setPageTransformer(true, new ScalePageTransformer());
        banner3.setPages(Utils.getImage(4));
    }

    @Override
    public Component createView(Context context, int integer,int index) {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_banner_image,null,false);
        Image image = (Image) component.findComponentById(ResourceTable.Id_img);
        Glide.with(context).load(integer)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(true)
                .transform(new RoundedCorners(20))
                .into(image);
        image.setClickedListener(component1 -> {

        });
        return component;
    }
}
